package com.foo.designpattern.state.state2;

import java.util.HashMap;
import java.util.Map;

/**
 * com.foo.designpattern.state
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-06-08 15:26.
 *
 * @Description
 */

public class VoteManager {
    /**
     * 记录当前每个用户对应的状态处理对象，每个用户当前的状态是不同的
     * Map<String, VoteState>对应Map<用户名, 当前对应的状态处理对象>
     */
    private Map<String, VoteState> mapState = new HashMap<String, VoteState>();
    /**
     * 记录用户投票的结果
     * Map<String, String>对应Map<用户名,投票的选项>
     */
    private Map<String, String> mapVote = new HashMap<String, String>();
    /**
     * 记录用户投票的次数
     * Map<String, Integer>对应Map<用户名,投票的次数>
     */
    private Map<String, Integer> mapVoteCount = new HashMap<String, Integer>();

    /**
     * 获取记录当前每个用户对应的状态处理对象
     *
     * @return mapState
     */
    public Map<String, VoteState> getMapState() {
        return mapState;
    }

    /**
     * 获取记录用户投票结果的Map
     *
     * @return mapVote
     */
    public Map<String, String> getMapVote() {
        return mapVote;
    }

    /**
     * 获取记录每个用户对应的投票次数的Map
     *
     * @return mapVoteCount
     */
    public Map<String, Integer> getMapVoteCount() {
        return mapVoteCount;
    }

    public void vote(String user, String voteItem) {
        Integer oldVoteCount = mapVoteCount.get(user);
        if (null == oldVoteCount) {
            oldVoteCount = 0;
        }
        oldVoteCount++;
        mapVoteCount.put(user, oldVoteCount);

        VoteState voteState = mapState.get(user);
        if (null == voteState) {
            // 在状态的处理类中进行状态的维护和转换
            voteState = new NormalVoteState();
        }
        voteState.vote(user, voteItem, this);

    }
}
